package com.xiaolin.sports;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.xiaolin.sports.base.BaseTest;
import com.xiaolin.sports.common.WechatConfigure;
import com.xiaolin.sports.job.WechatMediaJob;
import com.xiaolin.sports.model.WechatMedia;
import com.xiaolin.sports.service.IWechatMeidaService;
import com.xiaolin.sports.utils.HttpClientUtils;
import com.xiaolin.sports.utils.JsonUtils;
import com.xiaolin.sports.utils.StringUtil;
import com.xiaolin.sports.utils.TextUtil;
import com.xiaolin.sports.wechat.message.MediaListReq;
import com.xiaolin.sports.wechat.model.materialMedia.NewsItem;
import com.xiaolin.sports.wechat.model.materialMedia.NewsMedia;
import com.xiaolin.sports.wechat.model.materialMedia.NewsTotItem;

public class WechatMediaService extends BaseTest{

	@Autowired
	private IWechatMeidaService wechatMeidaService;
	
	@Test
	public void aa() {
		getMaterialMediaList();
	}
	
	public List<WechatMedia> getMaterialMediaList() {
		try {
			String url = TextUtil.format(WechatConfigure.getInstance().getMediaCount(), 
					"RjCp0OjKQmge7vuIzIRrM9FH8OzxzNq3_KgbSabPV0YenG_iRoqZVzX7VhR5K3f7Aa0I-VKwnuzzvRJQHfnrnp4VZJK8Gut-YMTVfHrsExb9gntsnwynuk2Fr3pSTAwHUSBcAJADBK");
			String resultStr = HttpClientUtils.doGet(url);
			JSONObject jsonObject = JSONObject.parseObject(resultStr);
			int newsCount = jsonObject.getIntValue("news_count");
			int page = (int)Math.ceil(Double.valueOf(newsCount)/Double.valueOf(19));
			for(int i = page; i >= 1; i--) {
				url = TextUtil.format(WechatConfigure.getInstance().getMediaList(), 
						"RjCp0OjKQmge7vuIzIRrM9FH8OzxzNq3_KgbSabPV0YenG_iRoqZVzX7VhR5K3f7Aa0I-VKwnuzzvRJQHfnrnp4VZJK8Gut-YMTVfHrsExb9gntsnwynuk2Fr3pSTAwHUSBcAJADBK");
				MediaListReq reqParam = new MediaListReq();
				reqParam.setCount(19);
				reqParam.setOffset((page * 19 - 19*(page-i+1)));
				resultStr = HttpClientUtils.postJson(url, reqParam);
				System.out.println("----:" + resultStr);
				NewsMedia newsMedia = JsonUtils.jsonToObject(resultStr, NewsMedia.class);
				for(NewsTotItem totItem : newsMedia.getItem()) {
					List<NewsItem> list = totItem.getContent().getNews_item();
					if(!StringUtil.isNullOrEmpty(list) && list.size() == 1) {
						NewsItem newsItem = list.get(0);
						WechatMedia media = new WechatMedia(); 
						media.setMediaId(newsItem.getThumb_media_id());
						media.setType(reqParam.getType());
						media.setTitle(newsItem.getTitle());
						media.setDigest(newsItem.getDigest());
						media.setPicUrl(newsItem.getThumb_url());
				        String author = newsItem.getAuthor();
				        String[] strings = author.split("\\|");
				        if(strings.length > 1) {
				        	media.setAuthorType(strings[0]);
				        	media.setAuthor(strings[1]);
				        	if(media.getAuthorType().equals("教练团队")) {
				        		media.setName(media.getTitle().replaceAll("教练介绍", ""));
				        	}
				        }else {
				        	media.setAuthor(author);
				        }
				        media.setContentSourceUrl(newsItem.getContent_source_url());
				        media.setUrl(newsItem.getUrl());
				        media.setUpdateTime(new Date());
				        wechatMeidaService.insertSelective(media);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}
}
